/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package crud;


import facade.ClienteFacade;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 *
 * @author vntraal
 */
public class CrudMasterViewCtrl {

    private int operation;
    private CrudMasterView crdmasterview;
    //private ClienteFacade cliFacade;

    public CrudMasterViewCtrl(CrudMasterView crudMasterView){
        operation = 0;        
        this.crdmasterview = crudMasterView;
        
        //fillJTableSearch();
        //fieldControl(operation);
    }


    public void insertRecord(){
        setOperation(1);
        buttonControl(getOperation());
        fieldControl(getOperation());
    }


    public void editRecord(){
        setOperation(2);
        buttonControl(getOperation());
        fieldControl(getOperation());
    }


    public void removeRecord(){
        setOperation(3);
    }


    public void searchRecord(){
        buttonControl(4);
        fieldControl(4);
    }


    public void saveRecordChanges(){
        buttonControl(5);
        fieldControl(5);

        /*

        ApplicationContext context = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        ClienteFacade instance = null;
        instance = (ClienteFacade)context.getBean("clienteFacade");







        ClienteFacade instance = null;

        instance = (ClienteFacade)context.getBean("clienteFacade");

        Clientes expResult = null;

        Clientes result = null;
        result  = instance.save(clientes);

        */



    }


    public void cancelRecordChanges(){
        buttonControl(6);
        fieldControl(6);
    }

    public void closeCrudWindow(){
        crdmasterview.dispose();
    }

    public void fieldControl(int operationCode){
     switch(operationCode){
            case 0:
            case 4:
            case 6:
                crdmasterview.getjPanelEdit().setVisible(false);
                crdmasterview.getjPanelSearch().setVisible(true);
                break;
            case 5:
                setFieldToReadOnly(true);
                break;
            default:
                crdmasterview.getjPanelEdit().setVisible(true);
                crdmasterview.getjPanelSearch().setVisible(false);
                setFieldToReadOnly(false);
                break;
     }
    }

    public void setFieldToReadOnly(boolean flag){
        /*
        jPanelEdit.getComponentCount();
        jPanelEdit.getComponent(0);

        for(int cpnIndx = 0;cpnIndx<jPanelEdit.getComponentCount();cpnIndx++){
            if (jPanelEdit.getComponent(cpnIndx) instanceof javax.swing.JTextField){

            }

        }*/
    }

    public void buttonControl(int buttonCode){
    /*
     * All buttons of the tool bar starts on the possible values for variable
     * buttonCode
     * 0 -> No button was selected
     * 1 -> New Record
     * 2 -> Edit Record
     * 3 -> Remove Record
     * 4 -> Search Record
     * 5 -> Save Record
     * 6 -> Cancel Record
     * 7 -> Exit
     */
        switch(buttonCode){
            case 0:
                crdmasterview.getjBtnSave().setEnabled(false);
                crdmasterview.getjBtnCancel().setEnabled(false);
                break;
            case 1:
            case 2:
            case 3:            
                crdmasterview.getjBtnSave().setEnabled(true);
                crdmasterview.getjBtnCancel().setEnabled(true);
                crdmasterview.getjBtnNew().setEnabled(false);
                crdmasterview.getjBtnEdit().setEnabled(false);
                crdmasterview.getjBtnRemove().setEnabled(false);
                crdmasterview.getjBtnSearch().setEnabled(false);
                break;
            case 4:
            case 5:
            case 6:
            case 7:
                crdmasterview.getjBtnSave().setEnabled(false);
                crdmasterview.getjBtnCancel().setEnabled(false);
                crdmasterview.getjBtnNew().setEnabled(true);
                crdmasterview.getjBtnEdit().setEnabled(true);
                crdmasterview.getjBtnRemove().setEnabled(true);
                crdmasterview.getjBtnSearch().setEnabled(true);
                break;
        }
    }    

    public List listAllRecords(){

        ApplicationContext context = new
                ClassPathXmlApplicationContext("applicationContext.xml");

        ClienteFacade instance = null;
        instance = (ClienteFacade)context.getBean("clienteFacade");
        List resultList = instance.listAll();
        System.out.println("Campo ID --> "+resultList.get(1));
        return resultList;
        
    }
    
    /**
     * @return the operation
     */
    public int getOperation() {
        return operation;
    }

    /**
     * @param operation the operation to set
     */
    public void setOperation(int operation) {
        this.operation = operation;
    }

   
}
